/*
 * @Author       : Bing Chen chenbing@iocharger.com
 * @Date         : 2024-09-02 01:04:00
 * @LastEditors  : Bing Chen chenbing@iocharger.com
 * @LastEditTime : 2025-03-15 13:01:31
 * @FilePath     : \openchaoji-corelib\shared\src\OpenChaoJi_utility_transport_gbt27930_a_connect.c
 * @Description  :
 *
 * Copyright (c) 2025 OpenChaoJi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the icense for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "OpenChaoJi_utility_transport_gbt27930_a.h"
#include "OpenChaoJi_utility_transport_gbt27930_a_private.h"

#if (OPENCHAOJI_CONFIG_ENABLE_GBT27930_A == CONFIG_YES)

struct OpenChaoJi_gbt27930_a_urmcb * OpenChaoJi_transport_gbt27930_a_urm_new(void * param)
{
	struct OpenChaoJi_gbt27930_a_urmcb * gbt27930_a_urmcb = NULL;

	gbt27930_a_urmcb = (struct OpenChaoJi_gbt27930_a_urmcb *)OpenChaoJi_os_malloc(sizeof(struct OpenChaoJi_gbt27930_a_urmcb));
	if (gbt27930_a_urmcb == NULL)
	{
		LOG_CRIT("transport urmcb malloc fail");
		return NULL;
	}

	gbt27930_a_urmcb->param = param;
	if (gbt27930_a_urmcb == NULL)
    {
        return NULL;
    }
    gbt27930_a_urmcb->param = param;

    return gbt27930_a_urmcb;
}

int16_t OpenChaoJi_transport_gbt27930_a_urm_free(struct OpenChaoJi_gbt27930_a_urmcb * urmcb)
{
	OpenChaoJi_os_free(urmcb);
	return 0;
}

int16_t OpenChaoJi_transport_gbt27930_a_urm_init(struct OpenChaoJi_gbt27930_a_urmcb * urmcb)
{
	if (urmcb == NULL){
		//error
		return -1;
	}
	urmcb->sm_recved_flag = 0;
	memset(urmcb->sm_recv_buf.data, 0x00, sizeof(urmcb->sm_recv_buf.data));

    return 0;
}

int16_t OpenChaoJi_transport_gbt27930_a_urm_bind(struct OpenChaoJi_gbt27930_a_urmcb * urmcb, int tag, uint8_t src_addr, uint8_t dest_addr,
																int (* can_send_func)(void * param, const struct can_pdu * send_pdu),
																int (* can_recv_func)(void * param, struct can_pdu * recv_pdu))
{
	if (urmcb == NULL){
		//error
		return -1;
	}

	if (can_send_func == NULL || can_recv_func == NULL){
		//error
		return -1;
	}

	urmcb->tag = tag;
	urmcb->src_addr = src_addr;
	urmcb->dest_addr = dest_addr;
	urmcb->can_output_func = can_send_func;
	urmcb->can_input_func = can_recv_func;

	return 0;
}

struct OpenChaoJi_gbt27930_a_rmcb * OpenChaoJi_transport_gbt27930_a_rm_new(void * param)
{
	struct OpenChaoJi_gbt27930_a_rmcb * gbt27930_a_rmcb = NULL;

	gbt27930_a_rmcb = (struct OpenChaoJi_gbt27930_a_rmcb *)OpenChaoJi_os_malloc(sizeof(struct OpenChaoJi_gbt27930_a_rmcb));
	if (gbt27930_a_rmcb == NULL)
	{
		LOG_CRIT("transport urmcb malloc fail");
		return NULL;
	}
    if (gbt27930_a_rmcb == NULL)
    {
        return NULL;
    }
    gbt27930_a_rmcb->param = param;
	gbt27930_a_rmcb->param = param;
	return gbt27930_a_rmcb;
}

int16_t OpenChaoJi_transport_gbt27930_a_rm_free(struct OpenChaoJi_gbt27930_a_rmcb * rmcb)
{
	OpenChaoJi_os_free(rmcb);

	return 0;
}

int16_t OpenChaoJi_transport_gbt27930_a_rm_init(struct OpenChaoJi_gbt27930_a_rmcb * rmcb)
{
	if (rmcb == NULL){
		//error
		return -1;
	}

	//需要确认长消息参数初始化
	rmcb->lm_send_lms_t1_intvl_ms = 10;//A类系统要求10ms
	rmcb->lm_send_lms_t2_intvl_ms = 100;
	rmcb->lm_send_lms_t3_intvl_ms = 5000;
	rmcb->lm_send_active_abort = 0;
	rmcb->lm_send_state = LM_SEND_IDLE;
	rmcb->lm_send_buf = NULL;


	rmcb->lm_recv_lms_t2_intvl_ms = 100;
	rmcb->lm_recv_lms_t3_intvl_ms = 5000;
	rmcb->lm_recved_flag = 0;
	rmcb->lm_recv_state = LM_RECV_S0_IDLE;
	rmcb->lm_recv_buf = NULL;

	return 0;
}

int16_t OpenChaoJi_transport_gbt27930_a_rm_bind(struct OpenChaoJi_gbt27930_a_rmcb * rmcb, int tag, uint8_t src_addr, uint8_t dest_addr,
																int (* can_send_func)(void * param, const struct can_pdu * send_pdu),
																int (* can_recv_func)(void * param, struct can_pdu * recv_pdu))
{
	if (rmcb == NULL){
		//error
		return -1;
	}

	if (can_send_func == NULL || can_recv_func == NULL){
		//error
		return -1;
	}

	rmcb->tag = tag;
	rmcb->src_addr = src_addr;
	rmcb->dest_addr = dest_addr;
	rmcb->can_output_func = can_send_func;
	rmcb->can_input_func = can_recv_func;

	return 0;
}

#endif
